//The MIT License (MIT)
//
//Copyright (c) 2018 dote
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of
//this software and associated documentation files (the "Software"), to deal in
//the Software without restriction, including without limitation the rights to
//use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
//the Software, and to permit persons to whom the Software is furnished to do so,
//subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
//FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
//COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
//IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
//CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.github.dote.mio.client;

import com.corundumstudio.socketio.AckCallback;
import com.corundumstudio.socketio.SocketIOClient;
import com.github.dote.mio.ack.AckSendTask;
import com.github.dote.mio.client.callback.SendCallback;
import com.github.dote.mio.protocal.Packet;
import com.github.dote.mio.protocal.PacketFactory;
import com.github.dote.mio.rtt.RTTHandler;
import com.github.dote.mio.util.ChannelUtil;
import com.github.dote.mio.util.CommonUtil;
import io.netty.channel.Channel;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class WebSocketClient implements IOClient {
    public final  static String PACKET_EVENT_NAME="WebSocketPakcetEventName";

    private SocketIOClient socketIOClient;

    private Map<String, Object> attrMap = new HashMap<>();

    public WebSocketClient(SocketIOClient socketIOClient) {
        this.socketIOClient = socketIOClient;
    }


    @Override
    public String getSessionId() {
        return socketIOClient.getSessionId().toString();
    }

    @Override
    public String getIp() {
        //return socketIOClient.get("ip");
        return socketIOClient.getHandshakeData().getAddress().getHostName();
    }

    @Override
    public void setAttr(String k, Object v) {
        if (attrMap.containsKey(k))
            attrMap.remove(k);
        attrMap.put(k, v);
    }

    @Override
    public boolean hasAttr(String k) {
        return attrMap.containsKey(k);
    }

    @Override
    public <T> T getAttr(String k) {
        return (T)attrMap.get(k);
    }

    @Override
    public <T> T getAttr(String k,Class<T> clazz) {
        Object obj=  attrMap.get(k);
        if(obj==null)
            return null;
        try {
            Method method = clazz.getMethod("valueOf",String.class);
            return (T) method.invoke(this,obj);
        } catch (Exception e) {
        }
        return (T)obj;
    }

    @Override
    public Map<String, Object> getAttrMap() {
        return attrMap;
    }

    @Override
    public void transfer(Packet packet, String toSessionId) {
        transfer(packet,toSessionId,null);
    }

    @Override
    public void transfer(Packet packet, String toSessionId, SendCallback sendCallback) {
        send(packet,sendCallback);
    }

    @Override
    public void send(Packet packet, SendCallback sendCallback) {
        if(isConnected()){
            socketIOClient.sendEvent(PACKET_EVENT_NAME,packet);
            if(sendCallback!=null){
                sendCallback.onSendResult(this,true);
            }
        }else{
            if(sendCallback!=null){
                sendCallback.onSendResult(this,false);
            }
        }

    }

    @Override
    public void send(Packet packet) {
        send(packet,null);
    }

    @Override
    public void send(String eventName, String fromSessionId, Object data, boolean ack, SendCallback sendCallback) {
        Packet packet = PacketFactory.createEventPacket(eventName, fromSessionId, data, ack);
        send(packet, sendCallback);
    }

    @Override
    public void send(String eventName, String fromSessionId, Object data, SendCallback sendCallback) {
        send(eventName, fromSessionId, data, true, sendCallback);
    }

    @Override
    public void send(String eventName, String fromSessionId, Object data, boolean ack) {
        send(eventName, fromSessionId, data, ack,null);
    }

    @Override
    public void send(String eventName, String fromSessionId, Object data) {
        send(eventName, fromSessionId, data,true);
    }

    @Override
    public void send(String eventName, Object data) {
        send(eventName,"0",data);
    }

    @Override
    public void setRTTHandler(RTTHandler rttHandler) {

    }

    @Override
    public RTTHandler getRTTHandler() {
        return null;
    }

    @Override
    public boolean isConnected() {
        return socketIOClient.isChannelOpen();
    }

    @Override
    public boolean close() {
        if(isConnected()){
            socketIOClient.disconnect();
            return true;
        }else  return false;
    }

    @Override
    public int compareTo(IOClient o) {
        return this.socketIOClient.getSessionId().compareTo(UUID.fromString(o.getSessionId()));
    }
}
